package com.moleq.mgnetwork;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class FolderMonitor extends Thread{

	private String dir;
	private int period;
	private boolean isError = false;
	private FileChangeCallback fileChangeCallback;



	public FolderMonitor(String dir, int period,FileChangeCallback fileChangeCallback) {
		this.dir = dir;
		this.period = period;
		this.fileChangeCallback = fileChangeCallback;
	}

	

	@Override
	public void run() {
		
		
		while (!isError ) {
			try {
				Thread.sleep(period * 1000);
				
				
				File dirFile = new File(dir);
				File[] files ;
				if (dirFile.isDirectory()) {
					
					files = dirFile.listFiles();
					
					if(files != null && fileChangeCallback != null)
						fileChangeCallback.onProcessFiles(files);
					
				}
				
			

			} catch (InterruptedException e) {
				System.out.println("对指定的文件路径进行监控时产生异常，异常信息为：" + e.getMessage());
				isError = true;
			}
		}
		
		
	}
	
	
	public void release(){
		isError = true;
	}

}
